//
//  AppDelegate.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/1/15.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "AppDelegate.h"
#import "WMERootViewController.h"
#import "WMEFindViewController.h"
#import "WMEClubViewController.h"
#import "WMEMySelfViewController.h"
#import "WMEShopViewController.h"
#import "WMEGuidePageViewController.h"
#import "WMEArticleFirstViewController.h"
#import <SMS_SDK/SMSSDK.h>
@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    /**<  启动页时间 */
    [NSThread sleepForTimeInterval:1.0];
    //短信验证SDK
    [SMSSDK registerApp:@"f3b828368320" withSecret:@"da774f725e3c501d9ef46d52ed0039b6"];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    BOOL isFirst = [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];

    if (!isFirst) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        WMEGuidePageViewController *yindaoyeVC = [[WMEGuidePageViewController alloc] init];

        //tabBar
        self.tabBarController = [[UITabBarController alloc] init];
        //首页
        WMERootViewController *root = [[WMERootViewController alloc] init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:root];
        
        root.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] tag:2000];
        //发现
        WMEFindViewController *find = [[WMEFindViewController alloc] init];
        UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:find];
        find.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"sousuo"] tag:3000];
        
        //商店
        WMEShopViewController *shop = [[WMEShopViewController alloc] init];
        shop.loadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://buy.duitang.com/buy/youliao/?from=app&_urlopentype=page&__urlopentype=pageweb"]];
        UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:shop];
        shop.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"商店" image:[UIImage imageNamed:@"shandian"] tag:3500];
        
        //club
        WMEClubViewController *club = [[WMEClubViewController alloc] init];
        UINavigationController *navi3 = [[UINavigationController alloc] initWithRootViewController:club];
        club.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"club" image:[UIImage imageNamed:@"club"] tag:4000];
        
        //mySelf
        WMEMySelfViewController *mySelf = [[WMEMySelfViewController alloc] init];
        //    UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:mySelf];
        mySelf.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"myself0"] tag:5000];
        WMEArticleFirstViewController *article = [[WMEArticleFirstViewController alloc] init];
        article.urlStr = @"http://api.meitianapp.com/api/v1/authors?filter=isDiscovery&limit=10&start=";
        UINavigationController *navi5 = [[UINavigationController alloc] initWithRootViewController:article];
        article.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"专栏" image:[UIImage imageNamed:@"book"] tag:6000];
        _tabBarController.viewControllers = @[navi, navi1, navi3, navi5, mySelf];
        _tabBarController.delegate = self;
        self.window.rootViewController = yindaoyeVC;
        _tabBarController.tabBar .translucent = NO;
        [navi3 release];
        [club release];
        [navi1 release];
        [find release];
        [navi release];
        [root release];
        [navi2 release];
        [shop release];
        [_tabBarController release];
        [_window release];

    }else{
        //tabBar
        self.tabBarController = [[UITabBarController alloc] init];
        //首页
        WMERootViewController *root = [[WMERootViewController alloc] init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:root];
        
        root.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] tag:2000];
        //发现
        WMEFindViewController *find = [[WMEFindViewController alloc] init];
        UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:find];
        find.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"sousuo"] tag:3000];
        
        //商店
//        WMEShopViewController *shop = [[WMEShopViewController alloc] init];
//        shop.loadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://buy.duitang.com/buy/youliao/?from=app&_urlopentype=page&__urlopentype=pageweb"]];
//        UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:shop];
//        shop.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"商店" image:[UIImage imageNamed:@"shandian"] tag:3500];
        
        //club
        WMEClubViewController *club = [[WMEClubViewController alloc] init];
        UINavigationController *navi3 = [[UINavigationController alloc] initWithRootViewController:club];
        club.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"club" image:[UIImage imageNamed:@"club"] tag:4000];
        
        //mySelf
        WMEMySelfViewController *mySelf = [[WMEMySelfViewController alloc] init];
        //    UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:mySelf];
        mySelf.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"myself0"] tag:5000];
        //article
        WMEArticleFirstViewController *article = [[WMEArticleFirstViewController alloc] init];
        article.urlStr = @"http://api.meitianapp.com/api/v1/authors?filter=isDiscovery&limit=10&start=";
        UINavigationController *navi5 = [[UINavigationController alloc] initWithRootViewController:article];
        article.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"专栏" image:[UIImage imageNamed:@"book"] tag:6000];
        
        _tabBarController.viewControllers = @[navi, navi1, navi3, navi5, mySelf];
        self.window.rootViewController = _tabBarController;
        _tabBarController.tabBar .translucent = NO;
        [navi3 release];
        [club release];
        [navi1 release];
        [find release];
        [navi release];
        [root release];
//        [navi2 release];
//        [shop release];
        [_tabBarController release];
        [_window release];
    }
    
    

    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults removeObjectForKey:@"success"];
//    [defaults removeObjectForKey:@"firstLaunch"];
    BOOL isNight = [defaults boolForKey:@"isNight"];
    isNight = NO;
    [defaults setBool:isNight forKey:@"isNight"];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
